import Vue from "vue"
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        count: 0,
        price: 4300,
        movieList: [],
    },
    mutations: {
        // mutation中的函数的第一个参数是当前的state
        increatment(state) {
            state.count++
        },
        increatmentN(state, { n }) {
            // mutation的第二个参数是载荷，接受commit的传参 vuex希望在载荷是个对象 才能接收多个参数
            console.log(n);
            state.count += n
        },
        decreatmentN(state, { n }) {

            state.count -= n

        },
        decreatmentNWait(state, { n }) {

            state.count -= n

        },
        setMovieList(state, re) {
            state.movieList = re.data.data.list
        }
    },
    actions: {
        decreatmentNWait({ commit }, payload) {
            setTimeout(() => {
                commit('decreatmentNWait', payload)
            })

        },
        async getMovieList({ commit }) {
            const re = await axios.get('https://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=11&page_id=2&ret_num=48&session=b9fd987164f6aa47fad266f57dffaa6a')
            commit('setMovieList', re)
        }
    },
    getters: {
        // getters和vue的计算属性相似
        // 方法1：
        myMovieList(state) {
            return state.movieList.filter(item => {
                return item.categories.includes('喜剧')

            })
        },
        // 方法2：
        // myMovieList(state) {
        //     return state.myMovieList = state.movieList.map(item => {
        //         if (item.categories.includes('喜剧')) return item.name

        //     })
        // }

        // 如果要给getters传值，就需要返回一个函数
         youMovieList(state){
            return ({n,m})=>{
                return state.movieList.filter(item=>{
                    
                    return item.name.length>n && item.name.length<=m
                })
            }
         }
  
    }
})